package com.techsoul.module.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.DateUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.basic.mapper.SupplierMapper;
import com.techsoul.module.basic.pojo.entity.Customer;
import com.techsoul.module.basic.pojo.entity.Supplier;
import com.techsoul.module.basic.pojo.param.StoreSearchParam;
import com.techsoul.module.basic.pojo.param.SupplierSaveParam;
import com.techsoul.module.basic.pojo.param.SupplierSearchParam;
import com.techsoul.module.basic.pojo.vo.StoreVo;
import com.techsoul.module.basic.pojo.vo.SupplierVo;
import com.techsoul.module.basic.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 供应商 Service实现
 *
 * @author liuen
 * @date 2021-01-26
 */
@Service
@Transactional(readOnly = true)
public class SupplierServiceImpl implements ISupplierService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private SupplierMapper mapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(SupplierSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        Supplier entity = new Supplier();
        BeanUtils.copyProperties(params, entity);
        if(user.getSite()!=null&&user.getSite()!=""){
            entity.setSite(user.getSite());
        }else{
            entity.setSite(user.getOrgId());
        }
        entity.setId(idWorker.nextId() + "").preInsert(user.getId());
        try{
            entity.setSupplierContractExpDate(DateUtils.parseDate(params.getSupplierContractExpDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        this.mapper.insert(entity);
        return entity.getId();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }
    /**
     * 获得地区企业总数
     *
     * @return
     */
    @Override
    public Integer findTotalCompany(String site) {
        return this.mapper.findTotalCompany(site);
    }
    /**
     * 批量作废
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer logicalDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            for(int i=0;i<idList.size();i++){
                String id = (String)idList.get(i);
                Supplier entityFromDb = this.mapper.selectById(id);

                UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();

                entityFromDb.preUpdate(user.getId());
                entityFromDb.setIsValid("0");
                this.mapper.updateById(entityFromDb);
            }
            return 1;
        } else {
            return 0;
        }
    }
    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(SupplierSaveParam params) {
        Supplier entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(user.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public SupplierVo findById(String id) {
        SupplierVo supplierVo = this.mapper.findById(id);
        return supplierVo;
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<SupplierVo> findAll(SupplierSearchParam params) {
        List<SupplierVo> supplierVos = this.mapper.findAll(params);
        return supplierVos;
    }

    @Override
    public List<SupplierVo> materialSupplierList(String materialNo) {
        List<SupplierVo> supplierVos = this.mapper.materialSupplierList(materialNo);
        return supplierVos;
    }
    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<SupplierVo> findAll(SupplierSearchParam params, Integer current, Integer pageSize) {
        IPage<SupplierVo> voIPage = this.mapper.findAll(new Page<>(current, pageSize), params);
        return voIPage;
    }

    /**
     * 合同到期列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<SupplierVo> findContractExppage(SupplierSearchParam params, Integer current, Integer pageSize){
        return this.mapper.findContractExppage(new Page<>(current, pageSize), params);
    }
    /**
     * 通过编码查询
     * @param code
     * @return
     */
    @Override
    public SupplierVo findByCode(String code) {
        return this.mapper.findByCode(code);
    }
    /**
     * 校验供应商编码
     * @param params
     * @return
     */
    @Override
    public Boolean checkCode(SupplierSearchParam params) {
        SupplierVo storeVo = this.findByCode(params.getSupplierCode());
        if (storeVo == null) {
            return true;
        } else {
            if (storeVo.getId().equals(params.getId())) {
                return true;
            } else {
                return false;
            }
        }
    }

    @Transactional(readOnly = false)
    @Override
    public void fixdata() {
        List<Supplier> suppliers = this.mapper.selectList(new QueryWrapper<>());
        for (Supplier supplier : suppliers) {
            this.mapper.updateById(supplier);
        }
    }
}
